<template>
  <a-drawer
    :maskClosable="false"
    :title="title"
    :width="width"
    placement="right"
    destroyOnClose
    :closable="false"
    @close="close"
    :visible="visible">
    <component
      ref="realForm"
      :is="component"
      v-if="formUrl"
      @ok="submitCallback"
      @close="closeCallback"
      @unButtonDisable="unButtonDisable"
      :pending="true"
      :borrowMoneyFlag="true"
      :disabled="disableSubmit"></component>
    <div class="drawer-footer">
      <CommonDrawerFooterButton
        startProcess="erpSealCertificationUseRecord: startProcess"
        :disableSubmit="disableSubmit"
        :isStaging="isStaging"
        :buttonDisable="buttonDisable"
        @handleCancel="handleCancel"
        @handleOk="handleOk"
        @beginProcess="beginProcess"
      ></CommonDrawerFooterButton>
      <!-- <a-popconfirm title="确定关闭弹窗吗?" @confirm="handleCancel" :getPopupContainer="(node) =>  node.parentNode">
        <a-button style="margin-bottom: 0;">关闭</a-button>
      </a-popconfirm>
      <a-button v-if="!disableSubmit" :disabled="buttonDisable"  @click="handleOk" type="primary" style="margin-bottom: 0;">保存</a-button> -->
    </div>
  </a-drawer>
</template>

<script>
import CommonDrawerFooterButton from '@/views/components/CommonDrawerFooterButton.vue';
export default {
    name: 'MyCommittedModules',
    components: {
      CommonDrawerFooterButton
    },
    props: {
      recordDetail: {
        type: Object,
        default: () => {
          return {}
        }
      },
      formUrl: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        title: '操作',
        width: 1000,
        visible: false,
        disableSubmit: false,
        buttonDisable: false,
        isStaging: true
      }
    },
    computed: {
      component: function() {
        // 动态加载组件
        if (!this.formUrl) {
          return {}
        } else {
          return resolve => require(['@/views' + this.formUrl + '.vue'], resolve)
        }
      }
    },
    methods: {
      add () {
        this.buttonDisable = false;
        this.visible = true
        this.$nextTick(() => {
          if(this.formUrl=='/erp/docManage/modules/noticeForm' || this.formUrl=='/erp/docManage/modules/opinionForm'){
            this.$refs.realForm.addOrEdit()
          } else {
            this.$refs.realForm.add();
          }
        })
      },
      edit (record) {
        this.buttonDisable = false;
        this.visible = true
        // record.id = record.dataId;
        this.timer = setTimeout(() => {
          this.$nextTick(() => {
            if(this.formUrl=='/erp/docManage/modules/noticeForm' || this.formUrl=='/erp/docManage/modules/opinionForm'){
              this.$refs.realForm.addOrEdit(record);
            } else {
              this.$refs.realForm.edit(record);
            }
          });
          if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
          }
        }, 300)
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      closeCallback () {
        this.close()
      },
      submitCallback(type, record) {
        this.$emit('ok', type, record);
        if (type == 'draft') {
          this.visible = false;
        }
        if (type == 'refresh') {
          this.visible = false;
        }
      },
      handleOk () {
        this.buttonDisable = true;
        if(this.formUrl=='/erp/docManage/modules/noticeForm' || this.formUrl=='/erp/docManage/modules/opinionForm'){
          this.$refs.realForm.submitForm(0);
        } else {
          this.$refs.realForm.submitForm('draft');
        }
      },
      handleCancel () {
        this.close()
      },
      // 点击发起流程
      beginProcess () {
        this.buttonDisable = true;
        if(this.formUrl=='/erp/docManage/modules/noticeForm' || this.formUrl=='/erp/docManage/modules/opinionForm'){
          this.$refs.realForm.submitForm(1);
        } else {
          this.$refs.realForm.submitForm('submit');
        }
      },
      unButtonDisable () {
        this.buttonDisable = false
      }
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
  .drawer-footer{
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
    z-index: 100000;
  }
</style>
